/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package phd.changeidentifiers.testing.display.graphs;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import phd.changeidentifiers.testing.display.TestDisplayApplication;
import phd.changeidentifiers.testing.display.frames.GraphPanel;
import phd.changeidentifiers.testing.display.objects.TimeStepDBObject;

/**
 *
 * @author Max
 */
public class GraphTestAreaDifference extends GraphPanel{
    private TimeStepDBObject[] timeSteps;
    private int redrawCount;
    public GraphTestAreaDifference(TimeStepDBObject[] timeSteps, int redrawCount){
        this.timeSteps = timeSteps;
        this.redrawCount = redrawCount;
        double maxY = 0d;
        for(TimeStepDBObject timeStep: timeSteps){
            if(timeStep.getSymmetricAreaDifference()>maxY)maxY = timeStep.getSymmetricAreaDifference();
        }
        this.setBackground(Color.WHITE);
        setXRange(0, this.timeSteps.length);
        setYRange(0, (int)Math.ceil(maxY));
        setScaleY(10);
        setScaleX(10);
        setxAxisTitle("Time Steps");
        setyAxisTitle("Area Difference");
        
    }
//    private void setSizes(Dimension size){
//        this.setSize(size);
//        this.setPreferredSize(size);
//        this.setMaximumSize(size);
//        this.setMinimumSize(size);
//    }
    
    @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 =  (Graphics2D) g;
        g2.setColor((TestDisplayApplication.GRAYSCALE?Color.BLACK:Color.red));
        Point point;
        Point previousPoint = null;

        Point actPoint;
        Point actPreviousPoint = null;
        double diff;
        double area = 0;
        double width;
        double height_to_start;
        double height_to_finish;
        double trapezoid_area;
        Stroke plain = new BasicStroke((TestDisplayApplication.LARGE?3f:1f));
        g2.setStroke(plain);
        for(int x = 0 ;x < getMaxX(); x++){
            diff = timeSteps[x].getSymmetricAreaDifference();
            actPoint = new Point(x, (int)Math.ceil(diff));
            point = getScreenPosition(actPoint);
            g2.fillOval(point.x-(TestDisplayApplication.LARGE?8:3),
                        point.y-(TestDisplayApplication.LARGE?8:3),
                        (TestDisplayApplication.LARGE?16:6),
                        (TestDisplayApplication.LARGE?16:6));
            if(previousPoint!=null){
                g2.setColor((TestDisplayApplication.GRAYSCALE?Color.DARK_GRAY:Color.red.darker()));
                g2.drawLine(previousPoint.x, previousPoint.y, point.x, point.y);
            }
            if(actPreviousPoint!=null){
                //If line is right to left this will be negative and give a negative
                //for my area so can just add the areas;
                width = actPoint.getX()-actPreviousPoint.getX();
                height_to_start = actPreviousPoint.getY();
                height_to_finish = actPoint.getY();
                trapezoid_area = (width/2)*(height_to_start+height_to_finish);
                area += trapezoid_area;
            }
            previousPoint = point;
            actPreviousPoint = actPoint;
        }
        Point areaPoint = getScreenPosition(2, -2);
        g2.drawString("Area under graph: "+area, areaPoint.x, areaPoint.y);
        Point redrawPoint = getScreenPosition(20, -2);
        g2.drawString("Redrew: "+redrawCount, redrawPoint.x, redrawPoint.y);

    }
    
   
}
